package cn.hfatec.shms.system.authResource.service;

import cn.hfatec.shms.security.VasInvocationSecurityMetadataSourceService;
import cn.hfatec.shms.system.authResource.dao.AuthResourceDao;
import cn.hfatec.shms.system.authResource.model.AuthResource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;



/**
 *
 * @author hfatec
 * @version 1.0
 *
 * the JAVA code is generate by middlegen
 *
 */
@Service("AuthResourceService")
public class AuthResourceService {

    //注入Dao层
    @Resource(name = "AuthResourceDao")
    private AuthResourceDao authResourceDao;

    @Resource(name = "vasSecurityMetadataSource")
    private VasInvocationSecurityMetadataSourceService vasInvocationSecurityMetadataSourceService;

    /**
     *
     * 根据权限ID，获取已分配资源列表
     *
     * @param authId
     * @return
     */
    @Transactional
    public List<String> queryAssignResourceList(String authId) {
        return authResourceDao.queryResourcesByAuth(authId);
    }

    /**
     *
     * 分配资源权限
     *
     * @param authId
     * @param ids
     * @param allIds
     */
    @Transactional
    public void assignResources(String authId, String[] ids, String[] allIds) {

        authResourceDao.deleteAuthResourceBatch(authId, allIds);
        if(ids == null) {
            ids = new String[]{};
        }
        for(String resourceId : ids) {
            AuthResource authResource = new AuthResource();
            authResource.setAuthId(authId);
            authResource.setResourceId(resourceId);
            authResourceDao.insert(authResource);
        }
        vasInvocationSecurityMetadataSourceService.loadResourceDefine();
    }
}